<!DOCTYPE html>
<html lang="en">
<head>
    <title>姿态设备管理</title>
    {% include 'admin/common/header.html' %}
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-form" action="" lay-filter="user-query-form">
            <div class="layui-form-item">
                <h2 class="layui-form-h2" style="text-align: center;">发布课程</h2>
            </div>
        </div>
    </div>
</div>


<div class="layui-card">
    <div class="layui-card-body">
        <div class="course-label">
            <div class="leftImg"><img alt="课程封面" class="imgStyle"
                                      src={{ courseInfo.cover }}>
            </div>
            <div class="rightCon">
                <p class="title">{{ courseInfo.TrainingName }}</p>
                <p class="teacher"><span style="padding-right: 20px;"><svg aria-hidden="true" class="svg-icon"
                                                                           name="mingcheng"></svg>
                                教师名称：{{ courseInfo.teacher_name }}</span>
                    <span style="padding-left: 20px;"><svg aria-hidden="true" class="svg-icon" name="number">
                                    <use></use></svg> 学生人数：{{ courseInfo.learner_number }}人</span></p>
                <p class="info">{{ courseInfo.detail }}</p>
            </div>
        </div>
    </div>
</div>


<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="call-table" lay-filter="call-table"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="user-toolbar">
    {% if authorize("admin:call_equip:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="pear-icon pear-icon-add"></i>
            新增
        </button>
    {% endif %}
    {% if authorize("admin:call_equip:remove") %}
        <button class="pear-btn pear-btn-md" lay-event="batchRemove">
            <i class="pear-icon pear-icon-ashbin"></i>
            删除
        </button>
    {% endif %}
</script>

{# 用户修改操作 #}
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="pear-icon pear-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
            class="pear-icon pear-icon-ashbin"></i>
    </button>
</script>

{% include 'admin/common/footer.html' %}
</body>
<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let popup = layui.popup
        let common = layui.common
        let courseId = {{ courseInfo.id }};
        let categoryId = {{ courseInfo.category }};
        let MODULE_PATH = '/course/details/videos/'

        // 表格数据
        if (categoryId == 1 || categoryId == 4) {
            cols = [
                [
                    {type: 'checkbox'},
                    {title: '视频名称', field: 'chapterTitle', align: 'center', width: 110},
                    {title: '视频编码', field: 'chapterID', align: 'center'},
                    {title: '视频描述', field: 'details', align: 'center'},
                    {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
                ]
            ]
            console.log("HELLO WORLD!")
        } else {
            cols = [
                [
                    {type: 'checkbox'},
                    {title: '视频名称', field: 'chapterTitle', align: 'center', width: 110},
                    {title: '视频编码', field: 'chapterID', align: 'center'},
                    {title: '视频描述', field: 'details', align: 'center'},
                    {title: '犯人性别', field: 'sex', align: 'center'},
                    {title: '犯罪类型', field: 'crime_type', align: 'center'},
                    {title: '犯人年龄', field: 'age', align: 'center'},
                    {title: '犯罪倾向', field: 'crime_tendency', align: 'center'},
                    {title: '精神状态', field: 'mental', align: 'center'},
                    {title: '犯罪情绪', field: 'emotion', align: 'center'},
                    {title: '视频描述', field: 'details', align: 'center'},
                    {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
                ]
            ]
        }


        // 渲染表格数据
        table.render({
            elem: '#call-table',
            url: MODULE_PATH + {{ courseInfo.id }},
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#user-toolbar', /*工具栏*/
            text: {none: '暂无章节信息'},
            defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports'] /*默认工具栏*/
        })

        //
        $('.user-group').click(function () {
            let group = $(this).attr('user-group')
            let field = form.val('user-query-form')
            if (group === '-1') {
                field.deptId = group
                $(this).removeClass('button-default')
                $(this).prev().removeClass('button-primary')
                $(this).prev().addClass('button-default')
                $(this).addClass('button-primary')
            } else {
                field.deptId = group
                $(this).removeClass('button-default')
                $(this).next().removeClass('button-primary')
                $(this).next().addClass('button-default')
                $(this).addClass('button-primary')
            }
            window.refresh(field)
        })

        form.on('submit(user-query)', function (data) {
            window.refresh(data.field)
            return false
        })


        window.refresh = function (param) {
            table.reload('call-table', {where: param})
        }


        //
        $('.user-group').click(function () {
            let group = $(this).attr('user-group')
            let field = form.val('user-query-form')
            if (group === '-1') {
                field.deptId = group
                $(this).removeClass('button-default')
                $(this).prev().removeClass('button-primary')
                $(this).prev().addClass('button-default')
                $(this).addClass('button-primary')
            } else {
                field.deptId = group
                $(this).removeClass('button-default')
                $(this).next().removeClass('button-primary')
                $(this).next().addClass('button-default')
                $(this).addClass('button-primary')
            }
            window.refresh(field)
        })

        table.on('tool(call-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj)
            } else if (obj.event === 'edit') {
                window.edit(obj)
            }
        })

        table.on('toolbar(call-table)', function (obj) {
            if (obj.event === 'add') {
                window.add()
            } else if (obj.event === 'refresh') {
                window.refresh()
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj)
            } else if (obj.event === 'collasped') {
                $('.user-left').toggleClass('user-collasped')
                $('.user-main').toggleClass('user-collasped')
                table.resize()
            }
        })

        form.on('submit(user-query)', function (data) {
            window.refresh(data.field)
            return false
        })

        form.on('submit(user-query)', function (data) {
            table.reload('call-table', {where: data.field})
            return false
        })

        form.on('switch(user-enable)', function (obj) {
            let operate
            if (obj.elem.checked) {
                operate = 'enable'
            } else {
                operate = 'disable'
            }
            let loading = layer.load()
            $.ajax({
                url: '/course/details/videos' + operate,
                data: JSON.stringify({userId: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading)
                    if (result.success) {
                        popup.success(result.msg)
                    } else {
                        popup.failure(result.msg)
                    }
                }
            })
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add'
            })
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '500px'],
                content: MODULE_PATH + 'edit?' +
                    'id=' + obj.data['id'] + '&' +
                    'code=' + obj.data['code'] + '&' +
                    'sn=' + obj.data['sn'] + '&' +
                    'name=' + obj.data['name'] + '&' +
                    'x=' + obj.data['x'] + '&' +
                    'y=' + obj.data['y'] + '&' +
                    'z=' + obj.data['z'] + '&' +
                    'rx=' + obj.data['rx'] + '&' +
                    'ry=' + obj.data['ry'] + '&' +
                    'rz=' + obj.data['rz'] + '&' +
                    'map_id=' + obj.data['map_id'] + '&' +
                    'dept_id=' + obj.data['dept_id'] + '&' +
                    'camera_id=' + obj.data['camera_id']
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该设备信息', {icon: 3, title: '提示'}, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'remove/' + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del()
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data
            if (data.length === 0) {
                layer.msg('未选中数据', {
                    icon: 3,
                    time: 1000
                })
                return false
            }
            var ids = []
            var hasCheck = table.checkStatus('call-table')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            {#console.log(ids);#}
            layer.confirm('确定要删除选中数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({

                    url: MODULE_PATH + 'batchRemove',
                    data: {ids: ids},
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('call-table')
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.refresh = function (param) {
            table.reload('call-table', {where: param})
        }
    })
</script>
<style>
    .course-label {
        min-height: 200px;
        padding: 20px;
        border-radius: 4px;
        margin: -10px;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
    }

    .leftImg {
        width: 300px;
        height: 200px;
        margin: 20px;
    }

    .leftImg .imgStyle {
        width: 300px;
        height: 200px;
        -o-object-fit: cover;
        object-fit: cover;
        display: block;
        border-radius: 4px;
        position: relative;
    }

    .rightCon {
        padding: 20px;
        margin: 10px;
    }

    .title {
        margin-top: 8px;
        font-size: 16px;
        font-weight: 500;
        color: #333;
    }

    .teacher {
        margin: 12px;
        padding: 12px 0;
        color: #999;
        font-size: 12px;
    }

    .info {
        font-size: 14px;
        color: #999;
        line-height: 22px;
        height: 66px;
        overflow: hidden;
        position: relative;
    }

    .svg-icon {
        width: 1em;
        height: 1em;
        vertical-align: -0.15em;
        fill: currentColor;
        overflow: hidden;
    }

</style>
</html>